using System;
namespace APP.HELPER.Utils.BarcodeLib
{
	internal class EAN8 : BarcodeCommon, IBarcode
	{
		private string[] EAN_CodeA = new string[]
		{
			"0001101",
			"0011001",
			"0010011",
			"0111101",
			"0100011",
			"0110001",
			"0101111",
			"0111011",
			"0110111",
			"0001011"
		};
		private string[] EAN_CodeC = new string[]
		{
			"1110010",
			"1100110",
			"1101100",
			"1000010",
			"1011100",
			"1001110",
			"1010000",
			"1000100",
			"1001000",
			"1110100"
		};
		public string Encoded_Value
		{
			get
			{
				return this.Encode_EAN8();
			}
		}
		public EAN8(string input)
		{
			this.Raw_Data = input;
			this.CheckDigit();
		}
		private string Encode_EAN8()
		{
			if (this.Raw_Data.Length != 8 && this.Raw_Data.Length != 7)
			{
				throw new Exception("EEAN8-1: Invalid data length. (7 or 8 numbers only)");
			}
			if (!BarcodeEdit.CheckNumericOnly(this.Raw_Data))
			{
				throw new Exception("EEAN8-2: Numeric only.");
			}
			string str = "101";
			for (int i = 0; i < this.Raw_Data.Length / 2; i++)
			{
				str += this.EAN_CodeA[int.Parse(this.Raw_Data[i].ToString())];
			}
			str += "01010";
			for (int i = this.Raw_Data.Length / 2; i < this.Raw_Data.Length; i++)
			{
				str += this.EAN_CodeC[int.Parse(this.Raw_Data[i].ToString())];
			}
			return str + "101";
		}
		private void CheckDigit()
		{
			if (this.Raw_Data.Length == 7)
			{
				int num = 0;
				int num2 = 0;
				for (int i = 0; i <= 6; i += 2)
				{
					num2 += int.Parse(this.Raw_Data.Substring(i, 1)) * 3;
				}
				for (int i = 1; i <= 5; i += 2)
				{
					num += int.Parse(this.Raw_Data.Substring(i, 1));
				}
				int num3 = num + num2;
				int num4 = num3 % 10;
				num4 = 10 - num4;
				if (num4 == 10)
				{
					num4 = 0;
				}
				this.Raw_Data += num4.ToString();
			}
		}
	}
}
